package com.punch.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.punch.common.dto.user.role.RoleMenuDTO;
import com.punch.common.model.SysMenu;
import com.punch.common.model.SysRole;
import com.punch.common.page.PageInfo;
import com.punch.common.query.user.role.RoleQueryInfo;
import com.punch.common.service.impl.SuperServiceImpl;
import com.punch.common.simple.redis.utils.RedisUtil;
import com.punch.common.tools.BasePage;
import com.punch.common.vo.user.role.RoleVO;
import com.punch.user.mapper.SysRoleMapper;
import com.punch.user.model.SysRoleMenu;
import com.punch.user.service.MenuService;
import com.punch.user.service.RoleMenuService;
import com.punch.user.service.RoleService;
import com.punch.user.util.MenuUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者 owen E-mail: 624191343@qq.com
 */
@Slf4j
@Service
public class RoleServiceImpl extends SuperServiceImpl<SysRoleMapper, SysRole> implements RoleService {

    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private MenuService menuService;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 角色分页
     *
     * @param queryInfo 查询条件
     * @return PageInfo<RoleVO>
     */
    @Override
    public PageInfo<RoleVO> queryPage(RoleQueryInfo queryInfo) {
        return BasePage.queryBasePage(queryInfo, params -> baseMapper.queryCount(params),
                params -> baseMapper.queryList(params));
    }

    /**
     * 角色菜单功能权限
     *
     * @param roleMenuDTO 批量角色修改表单
     * @return Long
     * @author wujj
     * @since 2020/6/5 14:26
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Long roleMenuPerm(RoleMenuDTO roleMenuDTO) {
        Long roleId = roleMenuDTO.getRoleId();
        //删除该角色
        UpdateWrapper<SysRoleMenu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysRoleMenu::getRoleId, roleId);
        roleMenuService.remove(updateWrapper);
        //插入角色菜单表
        if (CollUtil.isNotEmpty(roleMenuDTO.getMenuId())) {
            List<SysRoleMenu> insertList = new ArrayList<>();
            roleMenuDTO.getMenuId().forEach(menuId -> {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setRoleId(roleId);
                insertList.add(sysRoleMenu);
            });
            roleMenuService.saveBatch(insertList);
        }
        //设置角色id到redis中
        redisUtil.deleteValue("role:" + roleId + ":menuIds");
        List<Long> retSet = new ArrayList<>();
        List<SysMenu> allMenuList = menuService.list();
        List<SysMenu> roleMenuList = menuService.listByIds(roleMenuDTO.getMenuId());
        MenuUtil.handUserMenu(allMenuList, roleMenuList, retSet);
        redisUtil.leftPushList("role:" + roleId + ":menuIds", retSet, RoleServiceImpl::apply);
        return roleMenuDTO.getRoleId();
    }

    private static Object apply(Object t) {
        return t;
    }


    /**
     * 角色菜单功Id列表
     *
     * @param roleId 角色id
     * @return 菜单id
     * @author wujj
     * @since 2020/6/5 14:26
     */
    @Override
    public List<String> roleMenuList(Long roleId) {
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenu::getRoleId, roleId);
        List<SysRoleMenu> list = roleMenuService.list(queryWrapper);
        List<String> retList = new ArrayList<>();
        list.forEach(sysRoleMenu -> retList.add(String.valueOf(sysRoleMenu.getMenuId())));
        return retList;
    }


}
